package com.example.mystore.service.impl;



import com.example.mystore.mapper.AddressMapper;
import com.example.mystore.mapper.OrderMapper;
import com.example.mystore.mapper.OrderListMapper;
import com.example.mystore.mapper.UserMapper;
import com.example.mystore.model.*;
import com.example.mystore.model.dto.OrderDto;
import com.example.mystore.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderListMapper orderListMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public OrderDao getOrderDetails(Integer orderNo) {
        Order order = orderMapper.getOrderById(orderNo);
        if (order != null) {
            OrderList orderlist = orderListMapper.getOrderListsByOrderId(orderNo);
            return new OrderDao(order, orderlist);
        }
        return null;
    }
    @Override
    public void createOrder(OrderRequest orderRequest) {
        // 创建订单
        Order order = new Order();
        BigInteger orderNo = generateUniqueOrderNo();
        order.setOrderNo(orderNo);
        order.setUid(orderRequest.getUserId());
        order.setAddrId(orderRequest.getAddressId());
        order.setAmount(BigDecimal.valueOf(orderRequest.getTotalPrice()));
        order.setType(1); // 付款类型，1-在线支付，示例中固定为1
        order.setStatus(1); // 订单状态，1-未付款，示例中固定为1
        order.setFreight(0); // 运费，示例中固定为0
        order.setCreated(new Date());
        order.setUpdated(new Date());
        orderMapper.insertOrder(order); // 使用 orderMapper 保存订单

        // 创建订单详情
        OrderList orderList = new OrderList();
        orderList.setUid(orderRequest.getUserId());
        orderList.setOrderNo(orderNo); // 使用相同的订单号
        orderList.setGoodsId(orderRequest.getProductId());
        orderList.setGoodsName(orderRequest.getProductName());
        orderList.setIconUrl("11");
        orderList.setPrice(BigDecimal.valueOf(orderRequest.getProductPrice()));
        orderList.setQuantity(orderRequest.getQuantity());
        orderList.setTotalPrice(BigDecimal.valueOf(orderRequest.getProductPrice() * orderRequest.getQuantity())); // 计算订单项总价
        orderList.setCreated(new Date());
        orderList.setUpdated(new Date());
        orderListMapper.insertOrderList(orderList); // 使用 orderListMapper 保存订单详情
    }


    private BigInteger generateUniqueOrderNo() {
        Random random = new Random();
        BigInteger orderNo;
        do {
            // 生成一个范围在 1000 到 9999 的随机数
            orderNo = BigInteger.valueOf(random.nextInt(9000) + 1000);
        } while (orderMapper.existsOrderByOrderNo(orderNo)); // 检查订单号是否已存在

        return orderNo;
    }

    @Override
    public int deleteOrderById(int id) {
        return orderMapper.deleteOrderById(id);
    }

    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }



    @Override
    public Order getOrderByNo(int orderNo) {
        return orderMapper.selectOrderByNo(orderNo);
    }
    @Override
    public List<Order> getOrderById(Integer userId) {
        return orderMapper.selectOrderById(userId);
    }



    @Override
    public List<Order> getAllOrders() {
        return orderMapper.selectAllOrders();
    }

    @Override
    public OrderDto getOrderDetail(Integer id) {
        OrderDto orderDto = new OrderDto();
        Order orderById = orderMapper.getOrderById(id);
        Integer addrId = orderById.getAddrId();
        Integer uid = orderById.getUid();
        User user = userMapper.getUserById(uid);
        Address address = addressMapper.getAddressById(addrId);
        OrderList orderListById = orderListMapper.getOrderListsByOrderId(id);
        BeanUtils.copyProperties(orderById, orderDto);
        orderDto.setUser(user);
        orderDto.setAddress(address);
        orderDto.setOrderList(orderListById);
        return orderDto;
    }
}

